package easy;

public class Solution_897 {

    private TreeNode head = null, newRoot = null;

    public TreeNode increasingBST(TreeNode root) {
        dps(root);
        return newRoot;
    }

    private void dps(TreeNode node) {
        if (node == null) {
            return;
        }
        dps(node.left);
        node.left = null;
        if (head == null) {
            head = node;
            newRoot = node;
        } else {
            head.right = node;
            head = node;
        }
        dps(node.right);
    }

    public static void main(String[] args) {
        Solution_897 model = new Solution_897();
        TreeNode root = new TreeNode(5);
        root.left = new TreeNode(3);
        root.left.left = new TreeNode(2);
        root.left.left.left = new TreeNode(1);
        root.left.right = new TreeNode(4);
        root.right = new TreeNode(6);
        root.right.right = new TreeNode(8);
        root.right.right.left = new TreeNode(7);
        root.right.right.right = new TreeNode(9);
        TreeNode treeNode = model.increasingBST(root);
        System.out.println(treeNode);
    }

    static class TreeNode {

        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}
